import api from 'api/api.js';
import wx from "weixin-js-sdk";
import wechatUtil from "@/util/wechatUtil";
// 分页模块
export const pageMixin = {
    data() {
        return {
            list: [],
            loading: false,
            finished: false,
            pageNo: 1, //当前页面
            pageSize: 7 //每页的个数
        };
    },
    methods: {
        async onLoad() {
            console.log('执行onLoad');
            let res = await this.getCurrentData();
            // 这里考虑返出去一个成功还是失败
            if (res.code === '0000') {
                this.loading = false;
                console.log(res);
                console.log(res.result.documentList);

                this.list = [...this.list, ...res.result.documentList];
                let len = this.list.length;
                console.log(len);
                console.log(res.result.dataTotal <= len);
                if (res.result.dataTotal <= len) {
                    console.log('没有更多数据了');

                    // 没有数据了
                    this.finished = true;
                } else {
                    this.pageNo++;
                }

            }
        },

    },
}
// 发送短信
export const sendCodeMixin = {
    data() {
        return {
            code: "",
            time: 60 * 1000,
            showCountDown: true,

        };
    },
    methods: {
        async onCheckPhone() {
            let flag = await this.$validator.validate();
            if (!flag) {
                if (this.errors.has("phone")) {
                    this.$toast(this.errors.collect("phone")[0]);
                    return;
                }
                this.sendCode();
            } else {
                this.sendCode();
            }
        },
        // 发送短信验证码
        async sendCode() {
            let res = await api.sendSMS({
                mobile: this.phone,
                type: this.type // 1 注册，2是修改
            });
            if (res.code == "0000") {
                this.showCountDown = false;
            } else {
                this.$toast(res.message);
            }
        },
        // 倒计时完成
        finishCountDown() {
            console.log("倒计时完成");
            this.showCountDown = true;
        },
    },
}

// 分享
export const wxShareMixin = {
    methods: {
        async setWxConfig() {
            let wechat = await wechatUtil.init([
                "checkJsApi",
                "updateAppMessageShareData",
                "updateTimelineShareData"
            ]);
            wx.checkJsApi({
                jsApiList: ["updateAppMessageShareData", "updateTimelineShareData"],
                success: function (res) {
                    console.log("检查jsApiList结果：");
                    console.log(res);
                }
            });

            wx.ready(() => {
                const self = this;
                wx.updateAppMessageShareData({
                    title: self.wxShareData.title || "测试标题", // 分享标题
                    desc: self.wxShareData.desc || "测试描述", // 分享描述
                    link: location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                    imgUrl:
                        self.wxShareData.imgUrl ||
                        "http://app.ccvaed.com/defaultAvatar.png", // 分享图标
                    success: function () {
                        // 设置成功
                        console.log("设置分享给好友成功");
                    }
                });
                wx.onMenuShareTimeline({
                    title: self.wxShareData.title || "测试标题", // 分享标题
                    desc: self.wxShareData.desc || "测试描述", // 分享描述
                    link: location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                    imgUrl:
                        self.wxShareData.imgUrl ||
                        "http://app.ccvaed.com/defaultAvatar.png", // 分享图标
                    success: function () {
                        // 设置成功
                        console.log("设置分享朋友圈成功");
                    }
                });
            });
        }
    },
    created() {
        console.log('mixin文件页面created注册微信');
        debugger
        this.setWxConfig();
    }
}